import { Controller, Post, Body, Get } from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto, FindUserDto, UpdateUserDto, RemoveUserDto, RemoveBatchUserDto, LoginUserDto } from './dto/user.dto';
import { ExtraCtx } from '@/common/decorators/extractx.decorator';
import { ExtraContext } from '@/common/interfaces/extracontext.interface';
import { Authority } from '@/common/decorators/authority.decorator';
import { useResult } from '@/common/hooks/useResult';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post('login')
  async login(@Body() body: LoginUserDto, @ExtraCtx() extra: ExtraContext) {
    const res = await this.userService.login(body, extra);
    return useResult(res);
  }

  @Post('register')
  async register(@Body() body: CreateUserDto, @ExtraCtx() extra: ExtraContext) {
    const res = await this.userService.register(body, extra);
    return useResult(res);
  }

  @Post('findAll')
  @Authority('user:find')
  async findAll(@Body() body: FindUserDto) {
    const res = await this.userService.findAll(body);
    return useResult(res);
  }

  @Post('findOne')
  @Authority('user:find')
  async findOne(@Body() body: FindUserDto) {
    const res = await this.userService.findOne(body);
    return useResult(res);
  }

  @Get('hasAdmin')
  @Authority()
  async hasAdmin() {
    const res = await this.userService.hasAdmin();
    return useResult(res);
  }

  @Post('update')
  @Authority('user:update')
  async update(@Body() body: UpdateUserDto) {
    const res = await this.userService.update(body);
    return useResult(res);
  }

  @Post('remove')
  @Authority('user:remove', 'admin*')
  async remove(@Body() body: RemoveUserDto) {
    const res = await this.userService.remove(body);
    return useResult(res);
  }

  @Post('removeBatch')
  @Authority('user:remove')
  async removeBatch(@Body() body: RemoveBatchUserDto) {
    const res = await this.userService.removeBatch(body);
    return useResult(res);
  }
}
